package com.hyacinth.lotterysystem.common.pojo;

import com.hyacinth.lotterysystem.common.errocde.ErrorCode;
import com.hyacinth.lotterysystem.common.errocde.GlobalErrorCodeConstants;
import lombok.Data;
import org.springframework.util.Assert;

/**
 * 统一结果返回类
 *
 * @Author: Yun
 * @Date: 2025/6/10
 */
@Data
public class CommonResult<T> {
    // 返回的状态码
    private Integer code;

    // 返回的数据
    private T data;

    // 返回的信息
    private String msg;

    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> result = new CommonResult<>();
        result.code = GlobalErrorCodeConstants.SUCCESS.getCode();
        result.data = data;
        result.msg = "";
        return result;
    }

    public static <T> CommonResult<T> error(Integer code, String msg) {
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code),
                "code 不是错误的异常");
        CommonResult<T> result = new CommonResult<>();
        result.code = GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode();
        result.msg = msg;
        return result;
    }

    public static <T> CommonResult<T> error(ErrorCode errorCode) {
        return error(errorCode.getCode(), errorCode.getMsg());
    }

}
